﻿@using System.Text.RegularExpressions
@using Microsoft.Extensions.Localization
@inject IStringLocalizerFactory LocalizerFactory

<div class="p-4 mb-4 bg-white border border-gray-200 rounded-lg shadow-sm 2xl:col-span-2 dark:border-gray-700 sm:p-6 dark:bg-gray-800">
    <h3 class="mb-4 text-xl font-semibold dark:text-white">@SharedLocalizer["Notes"]</h3>
    <div class="dark:text-gray-300">
        @((MarkupString)ConvertUrlsToLinks(Notes).Replace(Environment.NewLine, "<br>"))
    </div>
</div>

@code {
    private IStringLocalizer SharedLocalizer => LocalizerFactory.Create("SharedResources", "AliasVault.Client");

    /// <summary>
    /// The notes to display.
    /// </summary>
    [Parameter]
    public string Notes { get; set; } = "";

    private static string ConvertUrlsToLinks(string text)
    {
        string urlPattern = @"(https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|www\.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9]+\.[^\s]{2,}|www\.[a-zA-Z0-9]+\.[^\s]{2,})";
        return Regex.Replace(text, urlPattern, match =>
        {
            string url = match.Value;
            if (!url.StartsWith("http://") && !url.StartsWith("https://"))
            {
                url = "http://" + url;
            }
            return $"<a href=\"{url}\" target=\"_blank\" rel=\"noopener noreferrer\" class=\"text-blue-500 hover:underline\">{match.Value}</a>";
        }, RegexOptions.None, TimeSpan.FromMilliseconds(200));
    }
}
